package com.hy.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.base.RespResult;

import com.hy.common.base.RespResultCode;
import com.hy.common.consts.CommonConst;
import com.hy.common.consts.RedisConst;
import com.hy.common.utils.CommonUtils;
import com.hy.entity.dto.DepartmentSearchDTO;
import com.hy.entity.dto.UnitSearchDTO;
import com.hy.entity.pojo.HyUnit;

import com.hy.entity.vo.HyUserVO;
import com.hy.mapper.HyUnitMapper;
import com.hy.service.HyUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("unitService")
public class HyUnitServiceImpl implements HyUnitService {


    @Autowired
    private HyUnitMapper unitMapper;

    @Resource
    private RedisTemplate redisTemplate;

    private Authentication authentication;

    public RespResult addUnit(HyUnit unit) {

        unit.setDel(1);
        unit.setCreateDate(new Timestamp(new Date().getTime()+ CommonConst.GMT_TIME));
        unitMapper.insert(unit);
        redisTemplate.opsForValue().set(RedisConst.UNIT_INCREMENT_RFID+unit.getUnitId(),0);
        return RespResult.getInstance(RespResultCode.Add_SUCCESS);
    }

    @Override
    public RespResult deleteUnit(List<Integer> ids) {
        List<String> propertyRFIDKeys=new ArrayList<>();
        int i = unitMapper.deleteBatchIds(ids);
        if (i==ids.size()){
            ids.stream().forEach(e->{
                propertyRFIDKeys.add(RedisConst.UNIT_INCREMENT_RFID+e);
            });
            redisTemplate.delete(propertyRFIDKeys);
            return RespResult.getInstance(RespResultCode.DEL_SUCCESS);

        }


        return RespResult.getInstance(RespResultCode.DEL_ERROR);
    }

    @Override
    public RespResult showUnit(UnitSearchDTO searchDTO) {
        searchDTO.setDel(1);
        authentication= SecurityContextHolder.getContext().getAuthentication();
        HyUserVO hyUserVO= (HyUserVO) authentication.getPrincipal();
        List<String> roleIds = hyUserVO.getRoles();
        Page<HyUnit> hyUnitPage;
        if (CommonUtils.isMember(roleIds,CommonConst.SYSTEM_ROLE_NAME)){

            Page<HyUnit> page=new Page<>(searchDTO.getCurrentPage(),searchDTO.getPageSize());
            LambdaQueryWrapper<HyUnit> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(HyUnit::getDel,1)
                    .like(searchDTO.getUnitName()!=null,HyUnit::getUnitName,searchDTO.getUnitName())
                    .gt(searchDTO.getStartDate()!=null,HyUnit::getCreateDate,searchDTO.getStartDate())
                    .lt(searchDTO.getEndDate()!=null,HyUnit::getCreateDate,searchDTO.getEndDate());
           hyUnitPage= unitMapper.selectPage(page, queryWrapper);
            return RespResult.success(hyUnitPage.getRecords())
                    .setCount(Long.valueOf(hyUnitPage.getTotal()).intValue());
        }else {
            HyUnit hyUnit = unitMapper.selectById(hyUserVO.getUnitId());
            List<HyUnit> hyUnit01=new ArrayList<>();
            hyUnit01.add(hyUnit);
            return RespResult.success(hyUnit01)
                    .setCount(1);
        }
    }

    @Override
    public RespResult updateUnit(HyUnit hyUnit) {

        authentication= SecurityContextHolder.getContext().getAuthentication();
        HyUserVO hyUserVO= (HyUserVO) authentication.getPrincipal();
        List<String> roleIds = hyUserVO.getRoles();
        if (CommonUtils.isMember(roleIds,CommonConst.SYSTEM_ROLE_NAME)){

            unitMapper.updateById(hyUnit);
        }else {
            if (hyUnit.getUnitId()!=hyUserVO.getUnitId()){
                return RespResult.error(500,"修改失败！只能修改自己单位的信息！");
            }
            unitMapper.updateById(hyUnit);
        }

        return RespResult.success();
    }

    @Override
    public RespResult getCanHandleUnit() {
        authentication= SecurityContextHolder.getContext().getAuthentication();
        HyUserVO hyUserVO= (HyUserVO) authentication.getPrincipal();
        List<String> roleIds = hyUserVO.getRoles();
        List<HyUnit> hyUnits;
        if (CommonUtils.isMember(roleIds,CommonConst.SYSTEM_ROLE_NAME)){
           QueryWrapper<HyUnit>  hyUnitQueryWrapper=new QueryWrapper<>();
           hyUnitQueryWrapper.eq("del",1);
            hyUnits=  unitMapper.selectList(hyUnitQueryWrapper);
        }else {
            DepartmentSearchDTO departmentSearchDTO = new DepartmentSearchDTO();
            departmentSearchDTO.setUnitId(hyUserVO.getUnitId());
            hyUnits=new ArrayList<>();
            hyUnits.add(unitMapper.selectById(hyUserVO.getUnitId()));
        }
        return RespResult.success(hyUnits);
    }
}
